import os
import sys
import re
import json
import time
from pathlib import Path
import base64
import requests

import numpy as np
import cv2
from ais.image import read_dcm_by_pydicom


def encode_base64_file(f):
    bs64_str = base64.b64encode(f.read())    # 打印图像转换base64格式的字符串
    print(bs64_str, type(bs64_str))   # 将base64格式的数据装换为二进制数据


def decode_base64_file(bs64_str, save_name):
    imgdata = base64.b64decode(bs64_str)    # 将二进制数据装换为图片
    with open(save_name, "wb") as f:
        f.write(imgdata)


def generate_cobbDet_request_jpg():
    url = "http://0.0.0.0:8081/api/AIS/uploadImg/cobbDet"

    root = Path('image')
    jpg_items = sorted(root.glob('*.jpg'))
    json_dict = {}
    json_dict['scannerType'] = 1
    json_dict['fileType'] = "JPG"
    seriesNo = "1.3.6.1.4.1.9590.100.1.2.101743131510015201839523365310215332980"
    json_dict['seriesNo'] = seriesNo
    sopNo = "0.1.2.2342342.0.1"
    # json_dict['seriesNo'] = seriesNo
    json_dict['images'] = []

    for file in jpg_items:
        # dcm_data = read_dcm_by_pydicom(str(dcm_file))
        dcm_dict = {}
        dcm_dict['seriesNo'] = seriesNo
        dcm_dict['sopNo'] =sopNo
        dcm_dict['position'] = "AP"
        dcm_dict['storeType'] = 'Base64'
        dcm_dict['content'] = None

        with open(str(file), 'rb') as f:
            bs64_str = base64.b64encode(f.read())
            bs64_str = bs64_str.decode('utf-8')
            dcm_dict['content'] = bs64_str
            # print(bs64_str)

        json_dict['images'].append(dcm_dict)

    json_dict['status'] = 1

    print(json_dict)

    st = time.time()

    json_dict = json.dumps(json_dict)
    res = requests.post(url, data=json_dict)
    # requests.post(url, json=json_dict)
    et = time.time()
    print("send post: ", url)
    print("send post cost: %3.2f" % (et - st))
    print(res.text)


def generate_cobbDet_request_dcm():
    url = "http://0.0.0.0:8081/api/AIS/uploadImg/cobbDet"

    root = Path('image')
    dcm_items = sorted(root.glob('*.dcm'))
    json_dict = {}
    json_dict['scannerType'] = 1
    json_dict['fileType'] = "DICOM"
    seriesNo = "1.3.6.1.4.1.9590.100.1.2.101743131510015201839523365310215332980"
    json_dict['seriesNo'] = seriesNo
    sopNo = "0.1.2.2342342.0.1"
    # json_dict['seriesNo'] = seriesNo
    json_dict['images'] = []

    for file in dcm_items:
        dcm_data = read_dcm_by_pydicom(str(file))
        dcm_dict = {}
        dcm_dict['seriesNo'] = dcm_data.SeriesInstanceUID
        dcm_dict['sopNo'] =dcm_data.SOPInstanceUID
        dcm_dict['position'] = dcm_data.PatientOrientation
        dcm_dict['storeType'] = 'Base64'
        dcm_dict['content'] = None

        with open(str(file), 'rb') as f:
            bs64_str = base64.b64encode(f.read())
            bs64_str = bs64_str.decode('utf-8')
            dcm_dict['content'] = bs64_str
            #print(bs64_str)

        json_dict['images'].append(dcm_dict)

    json_dict['status'] = 1

    print(json_dict)

    st = time.time()

    json_dict = json.dumps(json_dict)
    res = requests.post(url, data=json_dict)
    # requests.post(url, json=json_dict)
    et = time.time()
    print("send post: ", url)
    print("send post cost: %3.2f" % (et - st))
    print(res.text)


def main():
    generate_cobbDet_request_dcm()


if __name__ == '__main__':
    main()